home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mac-Source 1994 July
/
Mac-Source_July_1994.iso
/
C and C++
/
Libraries
/
GUSI
/
Examples
/
GUSISocketTest.c
< prev
next >
Wrap
Text File
|
1992-09-11
|
4KB
|
200 lines
/*********************************************************************
File : GUSI - Grand Unified Socket Interface
File : GUSISocketTest.c - Socket testing gear
Author : Matthias Neeracher <neeri@iis.ethz.ch>
Started : 25Jul92 Language : MPW C
08Sep92 MN Factor out more common code
Last : 08Sep92
*********************************************************************/
#include <GUSI.h>
#include <GUSITest.h>
#include <Types.h>
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <sys/errno.h>
int sock = -1;
int accsock = -1;
void Close(char, char, const char *)
{
if (close(sock)) {
printf("# close() returned error %s\n", Explain());
Where();
}
sock = accsock;
accsock = -1;
}
void Listen(char, char, const char *)
{
if (sock == -1) {
printf("# socket is not open\n");
Where();
return;
}
if (listen(sock, 5)) {
printf("# listen() returned error %s\n", Explain());
Where();
}
}
void Write(char, char, const char * line)
{
int len = strlen(line);
int part;
if (sock == -1) {
printf("# socket is not open\n");
Where();
return;
}
for (; len; len -= part, line += part) {
part = write(sock, line, len);
if (part < 0) {
printf("# write(\"%s\") returned error %s\n", line, Explain());
Where();
break;
}
}
}
void Read(char, char, const char *)
{
int len;
char buf[500];
char * line;
if (sock == -1) {
printf("# socket is not open\n");
Where();
return;
}
len = read(sock, buf, 500);
if (len < 0) {
printf("# read() returned error %s\n", Explain());
} else {
buf[len] = 0;
printf("# read() returned:\n");
for (line = strtok(buf, "\n\r"); line; line = strtok(nil, "\n\r"))
printf("# %s\n", line);
}
Where();
}
void Select(char, char, const char *)
{
int res;
fd_set rdfds;
fd_set wrfds;
fd_set exfds;
struct timeval delay;
if (sock == -1) {
printf("# socket is not open\n");
Where();
return;
}
FD_ZERO(&rdfds);
FD_ZERO(&wrfds);
FD_ZERO(&exfds);
FD_SET(sock, &rdfds);
FD_SET(sock, &wrfds);
FD_SET(sock, &exfds);
delay.tv_sec = 10;
delay.tv_usec = 0;
res = select(sock+1, &rdfds, &wrfds, &exfds, &delay);
if (res < 0) {
printf("# select() returned error %s\n", Explain());
} else if (!res) {
printf("# select() timed out\n");
} else {
printf(
"# select() returned %s%s%s\n",
FD_ISSET(sock, &rdfds) ? "canRead " : "",
FD_ISSET(sock, &wrfds) ? "canWrite " : "",
FD_ISSET(sock, &exfds) ? "exception " : "");
}
Where();
}
void TogBlk(char, char, const char *)
{
int block;
switch (fcntl(sock, F_GETFL, 0)) {
case 0:
block = 1;
break;
default:
block = 0;
break;
case -1:
printf("# fcntl(F_GETFL) returned error %s\n", Explain());
Where();
return;
}
if (ioctl(sock, FIONBIO, &block))
printf(
"# ioctl(FIONBIO, %s) returned error %s\n",
block ? "true" : "false",
Explain());
else
printf("# Socket is now %s\n", block ? "nonblocking" : "blocking");
Where();
}
void NRead(char, char, const char *)
{
int nread;
if (ioctl(sock, FIONREAD, &nread))
printf("# ioctl(FIONREAD) returned error %s\n", Explain());
else
printf("# %d bytes waiting to be read\n", nread);
Where();
}
void AddSocketCommands()
{
COMMAND('l', 'i', Listen, "", "Listen to socket");
COMMAND('c', 'l', Close, "", "Close socket");
COMMAND('w', 'r', Write, "text", "Write a line");
COMMAND('r', 'd', Read, "", "Read");
COMMAND('s', 'e', Select, "", "Select a socket");
COMMAND('t', 'b', TogBlk, "", "Toggle blocking status");
COMMAND('n', 'r', NRead, "", "Number of bytes to be read");
}
void CleanupSockets()
{
if (sock != -1)
close(sock);
if (accsock != -1)
close(accsock);
}